package com.backstage.service.Impl;

import com.backstage.mapper.SysPermissionMapper;
import com.backstage.mapper.SysRoleMapper;
import com.backstage.mapper.SysRolePermissionMapper;
import com.backstage.pojo.dto.Message;
import com.backstage.pojo.entity.SysPermission;
import com.backstage.pojo.entity.SysRole;
import com.backstage.pojo.entity.SysRolePermission;

import com.backstage.pojo.vo.SysRolePermissionVo;
import com.backstage.service.RolePermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.codec.net.QCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

@Service
public class RolePermissionServiceImpl implements RolePermissionService {
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysPermissionMapper sysPermissionMapper;
    @Autowired
    SysRolePermissionMapper sysRolePermissionMapper;

    List<SysRolePermissionVo> result;
    List<SysRolePermission> permissionList;

    @Override
    public Message<List<SysRolePermissionVo>> select(SysRolePermissionVo sysRolePermissionVo) {
        result = new ArrayList<>();
        Page<SysRolePermission> page = new Page<>();
        page.setCurrent(sysRolePermissionVo.getPage());
        page.setSize(sysRolePermissionVo.getRows());
        QueryWrapper<SysRolePermission> rolepermissionWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(sysRolePermissionVo.getRoleName())) {
            rolepermissionWrapper.eq("role_id", getSysRole(sysRolePermissionVo).getId());
        }
        IPage<SysRolePermission> resultPage = sysRolePermissionMapper.selectPage(page, rolepermissionWrapper);
        List<SysRolePermissionVo> result = permissionList(resultPage);
        Message<List<SysRolePermissionVo>> listMessage = Message.successMessage((long) result.size(), result);
        return listMessage;
    }

    @Override
    public Message<String> delete(SysRolePermission sysRolePermission) {

        QueryWrapper rolePermission = new QueryWrapper();
        rolePermission.eq("role_id", sysRolePermission.getRoleId());
        rolePermission.eq("permission_id", sysRolePermission.getPermissionId());
        sysRolePermissionMapper.delete(rolePermission);

        return Message.successMessage((long) 0, "删除成功");
    }

    @Override
    public Message<String> add(SysRolePermission sysRolePermission) {


        sysRolePermissionMapper.insert(sysRolePermission);

        return Message.successMessage((long) 0, "添加成功");

    }

    @Override
    public Message<String> update(Long roleid, List<SysRolePermission> updateP) {
        List<SysRolePermission> pers = permissions(roleid);

        for (int i = 0; i < updateP.size(); i++) {
            if (!pers.contains(updateP.get(i))) {
                Message<String> add = add(updateP.get(i));
            }
        }
        for (int i = 0; i < pers.size(); i++) {
            if (!updateP.contains(pers.get(i))) {
                Message<String> add = delete(pers.get(i));
            }
        }
        return Message.successMessage((long) 0, "ok");

    }

    @Override
    public Message<List<SysRolePermissionVo>> selectNo(SysRolePermissionVo sysRolePermissionVo) {
        List<SysPermission> permissions = sysPermissionMapper.selectList(null);
        List<SysRolePermissionVo> sysRolePermissionVos = new ArrayList<>();
        SysRole sysRole = getSysRole(sysRolePermissionVo);
        List<SysRolePermission> result1 = permissions(sysRole.getId());
        for (int i = 0; i < permissions.size(); i++) {
            boolean f = true;
            for (int j = 0; j < result1.size(); j++) {
                System.out.println(permissions.get(i).getId().equals(result1.get(j).getPermissionId()));
                if (permissions.get(i).getId().equals(result1.get(j).getPermissionId())) {
                    f = false;
                    break;
                }
            }
            if (f) {
                SysRolePermissionVo sysRolePermissionVo1 = new SysRolePermissionVo();
                SysRole role = getSysRole(sysRolePermissionVo);
                sysRolePermissionVo1.setRoleName(role.getName());
                sysRolePermissionVo1.setPermissionName(getSysPer(permissions.get(i)).getName());
                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setPermissionId(permissions.get(i).getId());
                sysRolePermission.setRoleId(role.getId());
                sysRolePermissionVo1.setSysRolePermission(sysRolePermission);
                sysRolePermissionVos.add(sysRolePermissionVo1);
            }
        }
        return Message.successMessage((long) sysRolePermissionVos.size(), sysRolePermissionVos);
    }

    public SysRole getSysRole(SysRolePermissionVo sysRolePermissionVo) {
        QueryWrapper<SysRole> rolewrapper = new QueryWrapper<>();
        rolewrapper.eq("name", sysRolePermissionVo.getRoleName());
        SysRole sysRole = sysRoleMapper.selectOne(rolewrapper);
        return sysRole;
    }

    public SysPermission getSysPer(SysPermission sysPermission) {
        QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
        SysPermission sysPermission1 = sysPermissionMapper.selectById(sysPermission.getId());
        return sysPermission1;
    }

    public List<SysRolePermissionVo> permissionList(IPage<SysRolePermission> resultPage) {
        permissionList = new ArrayList<>();
        for (int i = 0; i < resultPage.getRecords().size(); i++) {
            SysRolePermission sysRolePermission = resultPage.getRecords().get(i);
            SysRolePermissionVo sysRolePermissionVo1 = new SysRolePermissionVo();
            QueryWrapper<SysRole> rolewrapper = new QueryWrapper<>();
            QueryWrapper<Permission> permissionwrapper = new QueryWrapper<>();
            SysRole sysRole = sysRoleMapper.selectById(sysRolePermission.getRoleId());
            sysRolePermissionVo1.setRoleName(sysRole.getName());
            SysPermission sysPermission = sysPermissionMapper.selectById(sysRolePermission.getPermissionId());
            sysRolePermissionVo1.setPermissionName(sysPermission.getName());
            sysRolePermissionVo1.setSysRolePermission(sysRolePermission);
            result.add(sysRolePermissionVo1);
            permissionList.add(sysRolePermission);
        }
        return result;
    }

    public List<SysRolePermission> permissions(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id", id);
        List<SysRolePermission> list = sysRolePermissionMapper.selectList(wrapper);
        return list;
    }
}
